package io.xccit.hotelsystem.ui.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import io.xccit.hotelsystem.R;
import io.xccit.hotelsystem.ui.home.HomeFragment;
import io.xccit.hotelsystem.ui.hotel.HotelDetailActivity;

/**
 * 首页酒店适配器
 */
public class HotelAdapter extends RecyclerView.Adapter<HotelAdapter.ViewHolder> {

    private List<HomeFragment.Hotel> hotelList;

    public HotelAdapter(List<HomeFragment.Hotel> hotelList) {
        this.hotelList = hotelList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_hotel, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HomeFragment.Hotel hotel = hotelList.get(position);
        holder.hotelName.setText(hotel.getName());
        holder.hotelPrice.setText("¥" + hotel.getPrice());
        Glide.with(holder.itemView.getContext())
                .load(hotel.getImageRes())
                .into(holder.hotelImage);
                
        holder.itemView.setOnClickListener(v -> {
            Context context = v.getContext();
            Intent intent = new Intent(context, HotelDetailActivity.class);
            intent.putExtra("hotel_id", hotel.getId());
            intent.putExtra("hotel_name", hotel.getName());
            intent.putExtra("hotel_price", hotel.getPrice());
            intent.putExtra("hotel_image", hotel.getImageRes());
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return hotelList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView hotelImage;
        TextView hotelName;
        TextView hotelPrice;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            hotelImage = itemView.findViewById(R.id.hotel_image);
            hotelName = itemView.findViewById(R.id.hotel_name);
            hotelPrice = itemView.findViewById(R.id.hotel_price);
        }
    }

    public void setHotelList(List<HomeFragment.Hotel> hotelList) {
        this.hotelList = hotelList;
    }
}